<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use App\PatientCase;
use Redirect, Input;
use App\User;
use App\PatientInfo;
use DB, Log;
use Session;

class PatientCaseController extends Controller {
	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index() {

	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create(Request $request, $id) {
		$protocal_name = "mediview://";

		$patient = PatientInfo::find($id);
		$doctor = Auth::user();
		$case_id = DB::table('patient_case') -> max('id') + 1;
		$arr_doctor_info = array('doctor_name' => $doctor -> name, 'doctor_id' => $doctor -> id);
		$patient_full_name = $patient -> last_name . " " . $patient -> first_name;
		$patient_sex = 0;
		if ($patient -> sex == "male") {
			$patient_sex = 1;
		}
		$token = csrf_token();

		$arr_mediview_protocal = array('case_id' => $case_id, 'XSRF_Token' => $token, 'user_name' => $patient_full_name, 'sex' => $patient_sex, 'birthday' => '1970-01-01 ', 'doctor_info' => $arr_doctor_info, 'address' => $patient -> address, 'user_id' => $patient -> id, );

		$json_mediview_protocal = json_encode($arr_mediview_protocal);
		$base64href = base64_encode($json_mediview_protocal);
		$medihref = $protocal_name . $base64href;

		return view('user.case.create') -> with('patient', $patient) -> with('medihref', $medihref) -> with('case_id', $case_id);
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store(Request $request) {

	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id) {
		//

	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit(Request $request, $patient_id, $case_id) {
		//
		$case = PatientCase::find($case_id);
		$case_imgs_os = DB::table('picture') -> where("case_id", "=", $case_id) -> where("left_or_right", "=", "OS") -> get();
		$case_imgs_od = DB::table('picture') -> where("case_id", "=", $case_id) -> where("left_or_right", "=", "OD") -> get();

		return view('user.case.edit') -> with('case', $case) -> with('imgs_os', $case_imgs_os) -> with('imgs_od', $case_imgs_od);
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update(Request $request, $id) {
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id) {

	}

	public function printPreview(Request $request, $patient_id, $case_id) {
		$user_id = session::get('userId');
		$user = User::find($user_id);
		$patient = PatientInfo::find($patient_id);
		$case = PatientCase::find($case_id);
		$case_imgs_os = DB::table('picture') -> where("case_id", "=", $case_id) -> where("left_or_right", "=", "OS") -> get();
		$case_imgs_od = DB::table('picture') -> where("case_id", "=", $case_id) -> where("left_or_right", "=", "OD") -> get();
		$date = date("Y-m-d");
		return view('user.case.print') ->with('user',$user)-> with('patient', $patient) -> with('case', $case) -> with('imgs_os', $case_imgs_os) -> with('imgs_od', $case_imgs_od)->with('date',$date);
	}

}
